<?php
namespace app\admin\controller;
use \app\admin\model\Member;
use \app\admin\model\Comment;
use \app\admin\model\Order;
use \app\admin\model\Shop;
use \app\admin\model\Menus;
use \app\admin\model\MemberMenus;
use \app\admin\controller\BaseController;
use \think\Controller;
use Think\Db;

class Index extends BaseController {

	public function index(Member $member,Menus $menus,MemberMenus $member_menus )
	{
		$this->isLogin();
		$member_view_list = $member_menus->where(['auth_name' => session('auth_name'),])->field('menus_id')->select()->toArray();
        $member_view_menus_id = explode(',', $member_view_list[0]['menus_id']);
        $member_view_menus_list = $menus->where('id', 'in', $member_view_menus_id)->where('isshow',0)->field('id, name')->where([ 'parent_id' => '0'])->select()->toArray();
        foreach ($member_view_menus_list as $key => $value) {
            $v = $menus->where(['parent_id' => $value['id']])->field('name, url')->select()->toArray();
            $member_view_menus_list[$key]['min'] = $v;
        }
        unset($member_view_menus_list[0]);
		$list = $member->where('id', '=', session('member_id'))->select()->toArray();
		return view('', compact('list', 'member_view_menus_list'));
	}
	
	//首页
	public function select(Member $member,Order $order)
	{
		$this->isLogin();
		$list = [];
        $uid = session('member_id');
        if(session('auth_name') == 'member'){
            $where = ['member_id' => $uid];
        }else{
            $where = [];
        }
		$list = $member->where('id', '=', $uid)->where($where)->select()->toArray();
        $order['order_deal'] = $this->getPendingOrder(); //待发货理订单
        $order['order_void'] = $this->getReturnOrder(); //退款订单

        $shop = new Shop();
		$shop['shop_out_num'] = $shop->where(['stock_num' => 0, 'is_use'=>0,'audit'=>1, 'shop_delete' => 0])->where($where)->count(); //缺货登记
		$shop['shop_min_num'] = $shop->where(['member_id' => $uid, 'is_use'=>0,'audit'=>1,'shop_delete' => 0])->where($where)->select()->toArray(); //预警库存

		for($i = 0; $i < count($shop['shop_min_num']); $i++){
			$j = 0;
			if($shop['shop_min_num'][$i]['stock_num'] < $shop['shop_min_num'][$i]['shop_min_num']){
				$list['shop_min_num'] = $shop['shop_min_num'][$i];
			}
			$j++;
		}

		$shop['shop_is_heat'] = $shop->where(['is_heat' => 1,'is_use'=>0,'audit'=>1, 'shop_delete' => 0])->where($where)->count(); //热销品
		$promotion = $shop->where(['is_heat' => 1,'is_use'=>0,'audit'=>1, 'shop_delete' => 0])->where($where)->select()->toArray(); //促销商品数

        foreach($promotion as $value){
            if($value['shop_end_money'] < $value['shop_start_money']){
                $shop['shop_is_promotion'] = $value;

            }
        }
        $last_week = mktime(0, 0 , 0,date("m"),date("d")-7,date("Y")); //上周开始时间
        $week = mktime(23, 59, 59,date("m"),date("d"),date("Y")); //一周结束时间
        $shop['shop_new_num'] = $shop->where(['is_use'=>0,'audit'=>1,'shop_delete' => 0])->where($where)->where('create_time','between', [$last_week,$week])->count(); //新品数量
        $shop['shop_is_add'] = $shop->where(['is_use'=>0,'audit'=>1, 'shop_delete' => 0])->where($where)->count(); //上架商品
		$shop['shop_out_add'] = $shop->where([ 'is_use' => 1,'audit'=>1, 'shop_delete' => 0])->where($where)->count(); //下架商品
		$shop['shop_no_add'] = $shop->where(['shop_delete' => 1])->where($where)->count(); //删除商品

        $order['order_determined'] = $this->pendingPaymentOrder(); //待付款
        $order['order_delivered'] = $this->pendingDeliveryOrder(); //待发货
        $order['order_no_money'] = $this->toBeDeterminedOrder(); //待确定
        $order['order_over'] = $this->completedOrder(); //已完成
        $order['order_user_remind'] = $this->userRemindOrder(); //用户提醒

        $yester_day = mktime(0, 0 , 0,date("m"),date("d")-1,date("Y")); //昨日时间
        $data = mktime(23, 59 , 59,date("m"),date("d"),date("Y")); //今日
		$last_month = mktime(0, 0 , 0,date("m")-1,1,date("Y")); //上月开始时间
		$month = mktime(23,59,59,date("m") ,0,date("Y")); //上月结束
        $before_yesterday = mktime(0, 0 , 0,date("m"),date("d")-2,date("Y")); //昨日时间
        $order['order_today'] = $this->getDateOrder($yester_day,$data); //昨日订单数量
        $order['order_day'] = $this->getDateOrder($before_yesterday,$yester_day); //昨日订单数量
		$order['order_week'] =$this->getDateOrder($last_week,$week);  //上周订单数量
		$order['order_month'] = $this->getDateOrder($last_month,$month); //上月订单数量

        $list['order_deal'] = $order['order_deal'];
		$list['order_void'] = $order['order_void'];
		$list['shop_out_num'] = $shop['shop_out_num'];
		$list['shop_is_heat'] = $shop['shop_is_heat'];
		$list['shop_min_num'] = count($shop['shop_min_num']);
		$list['shop_is_promotion'] = count($shop['shop_is_promotion']);
		$list['shop_new_num'] = $shop['shop_new_num'];
		$list['shop_is_add'] = $shop['shop_is_add'];
		$list['shop_out_add'] = $shop['shop_out_add'];
		$list['shop_no_add'] = $shop['shop_no_add'];
//		$list['order_no_comment'] = $order['order_no_comment'];
		$list['order_determined'] = $order['order_determined'];
		$list['order_delivered'] = $order['order_delivered'];
		$list['order_user_remind'] = $order['order_user_remind'];
		$list['order_no_money'] = $order['order_no_money'];
		$list['order_today'] = count($order['order_today']);
		$list['order_over'] = $order['order_over'];
		$list['order_day_num'] = count($order['order_day']);

        if($order['order_day']) {
            $list['order_day_money'] = 0;
			foreach ($order['order_day'] as $key => $value) {
				$list['order_day_money'] += intval($value['order_money']);
			}
		} else {
			$list['order_day_money'] = 0;
		}

        if($order['order_today']) {
            $list['order_today_money'] = 0;
            foreach ($order['order_today'] as $key => $value) {
                $list['order_today_money'] += intval($value['order_money']);
            }
        } else {
            $list['order_today_money'] = 0;
        }

		$list['order_week_num'] = count($order['order_week']);
		if($order['order_week']) {
            $list['order_week_money'] = 0;
			foreach ($order['order_week'] as $key => $value) {
				$list['order_week_money'] += intval($value['order_money']);
			}
		} else {
			$list['order_week_money'] = 0;
		}

		$list['order_month_num'] = count($order['order_month']);
		if($order['order_month']) {
            $list['order_month_money'] = 0;
			foreach ($order['order_month'] as $value) {
                $list['order_month_money'] += intval($value['order_money']);
			}
		} else {
			$list['order_month_money'] = 0;
		}
		return view('', compact('list'));

	}
	public function whereHandel()
    {
	    if(session('auth_name')  == 'member'){
	        $isWhere = ['b.member_id' => session('member_id')];
        }else{
            $isWhere = [ ];
        }
	    return $isWhere;
    }

    /**
     * 商家待发货订单
     * @param $uid
     * @return false|mixed|\PDOStatement|string|\think\Collection
     */
	public function getPendingOrder()
	{
        return Db::table('sdb_order')->alias('a')
            ->join('sdb_order_shop b','a.order_number = b.order_number')
            ->where($this->whereHandel())
            ->where(['order_staff' => 2, 'freight_staff' => 0])
            ->count();
    }
    //商家退款处理中的订单
    public function getReturnOrder()
    {
        return Db::table('sdb_order')->alias('a')
            ->where('order_staff' , 5 )
            ->join('sdb_order_shop b','a.order_number = b.order_number')
            ->where($this->whereHandel())
            ->count();
    }
    //商家待付款订单
    public function pendingPaymentOrder(){
        return Db::table('sdb_order')->alias('a')
            ->where('order_staff' , 1 )
            ->where('freight_staff' , 0 )
            ->join('sdb_order_shop b','a.order_number = b.order_number')
            ->where($this->whereHandel())
            ->count();
    }
    //商家代发货订单
    public function pendingDeliveryOrder(){
        return Db::table('sdb_order')->alias('a')
            ->where('order_staff' , 2)
            ->where('freight_staff' , 0 )
            ->join('sdb_order_shop b','a.order_number = b.order_number')
            ->where($this->whereHandel())
            ->count();
    }
    //商家待确定订单
    public function toBeDeterminedOrder(){
        return Db::table('sdb_order')->alias('a')
            ->where('order_staff' , 2)
            ->where('freight_staff' , 2 )
            ->join('sdb_order_shop b','a.order_number = b.order_number')
            ->where($this->whereHandel())
            ->count();
    }
    //商家已完成订单
    public function completedOrder(){
        return Db::table('sdb_order')->alias('a')
            ->where('order_staff' , 3)
            ->where('freight_staff' , 2 )
            ->join('sdb_order_shop b','a.order_number = b.order_number')
            ->where($this->whereHandel())
            ->count();
    }
    //商家买家提醒订单
    public function userRemindOrder(){
        return Db::table('sdb_order')->alias('a')
            ->where('order_staff' , 2)
            ->where('freight_staff' , 0 )
            ->where('user_remind' , 1 )
            ->join('sdb_order_shop b','a.order_number = b.order_number')
            ->where($this->whereHandel())
            ->count();
    }
    //商家昨日、上周、上月的订单
    public function getDateOrder($start,$end)
    {
        return Db::table('sdb_order')->alias('a')->where('order_staff' , 3 )->where('order_use' ,0)
            ->join('sdb_order_shop b','a.order_number = b.order_number')
            ->where($this->whereHandel())
            ->where('a.create_time','between',[$start, $end])
            ->select();
    }
//    public function t()
//    {
//        $yester_day = mktime(0, 0 , 0,date("m"),date("d")-1,date("Y")); //昨日时间
//        $data = mktime(23, 59 , 59,date("m"),date("d"),date("Y")); //今日
//        dump(date('Y-m-d H:i:s',$yester_day));
//        dump(date('Y-m-d H:i:s',$data));die;
//    }

}